package com.example.wssapp;

import java.util.ArrayList;
import java.util.List;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wssapp.adapter.MyPageAdapter;
import com.example.wssapp.fragment.TextFragment;

public class MenuBaActivity extends FragmentActivity implements
		View.OnClickListener {

	private List<TextView> items = new ArrayList<TextView>();
	private ViewPager viewPager;
	private ImageView image;
	private int currentIndex = 0; // 当前页编号
	private int bmpW; // 横幅图片宽度
	private int offset; // 图片移动的偏移量

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置 activity 无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_menu_ba);

		initView();
		initImage();
		initViewPager();
	}

	private void initView() {
		TextView mywork = (TextView) findViewById(R.id.mywokr);
		TextView myapp = (TextView) findViewById(R.id.myapp);

		items.add(mywork);
		items.add(myapp);
		findViewById(R.id.menu1).setOnClickListener(this);
		findViewById(R.id.menu2).setOnClickListener(this);
	}

	private void initImage() {
		image = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.cursor)
				.getWidth();

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (screenW / items.size() - bmpW) / 2;

		Matrix matrix = new Matrix();
		matrix.setTranslate(offset, 0);
		image.setImageMatrix(matrix);
	}

	private void initViewPager() {
		viewPager = (ViewPager) findViewById(R.id.id_viewpager);

		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new TextFragment("这是第一个页面"));
		fragments.add(new TextFragment("这是另外一个页面"));

		viewPager.setAdapter(new MyPageAdapter(getSupportFragmentManager(),
				fragments));
		changeMenu(0);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				changeMenu(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		int position = -1;
		if (id == R.id.menu1) {
			position = 0;
		} else if (id == R.id.menu2) {
			position = 1;
		}

		if (position >= 0) {
			changeMenu(position);
		}
	}

	// 菜单切换
	private void changeMenu(int position) {
		for (TextView tv : items) {
			tv.setTextColor(Color.BLACK);
		}
		items.get(position).setTextColor(Color.RED);

		int one = offset * 2 + bmpW;// 两个相邻页面的偏移量
		int fromX = currentIndex * one;
		int toX = position * one;
		Animation animation = new TranslateAnimation(fromX, toX, 0, 0);

		currentIndex = position;
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(200);// 动画持续时间200ms
		image.startAnimation(animation);

		viewPager.setCurrentItem(position);
	}
}
